
package com.yimixian.app.data;

import com.ta.utdid2.android.utils.StringUtils;
import com.yimixian.app.YMXApplication;
import com.yimixian.app.activity.YMXMainActivity;
import com.yimixian.app.common.CacheKeys;
import com.yimixian.app.model.AdvertInfo;
import com.yimixian.app.model.AppConfig;
import com.yimixian.app.util.SharedPreferencesHelper;

import android.content.SharedPreferences;

import java.util.HashMap;
import java.util.Map;

public class DataManager {

    private static DataManager mInstance;

    public static DataManager getInstance() {
        if (mInstance == null) {
            mInstance = new DataManager();
        }

        return mInstance;
    }

    private final Map<String, Object> mCache;
    private final Map<String, Long> mExpireTimeMap;

    public DataManager() {
        mCache = new HashMap<>();
        mExpireTimeMap = new HashMap<>();
    }

    /**
     * 存储广告页面有关信息
     */
    public synchronized void saveAdInfo(AdvertInfo advertInfo) {
        SharedPreferences mSharedPreferences = YMXApplication.getInstance().mSharedPreferences;
        if (mSharedPreferences != null) {
            if (advertInfo.url != null)
                mSharedPreferences.edit().putString(CacheKeys.LAST_ADVERT_URL, advertInfo.url)
                        .commit();
            if (advertInfo.region != null)
                mSharedPreferences.edit().putString(CacheKeys.LAST_ADVERT_ID, advertInfo.region)
                        .commit();
        }
    }

    /**
     * 取出广告页面有关信息
     */
    public synchronized AdvertInfo getAdInfo() {
        AdvertInfo advertInfo = null;
        SharedPreferences mSharedPreferences = YMXApplication.getInstance().mSharedPreferences;
        if (mSharedPreferences != null) {
            advertInfo = new AdvertInfo();
            advertInfo.url = mSharedPreferences.getString(CacheKeys.LAST_ADVERT_URL, null);
            advertInfo.region = mSharedPreferences.getString(CacheKeys.LAST_ADVERT_ID, null);
        }
        return advertInfo;
    }

    public synchronized void onMainActivityCreate(SharedPreferences sharedPreferences) {
        String token = sharedPreferences.getString(CacheKeys.TOKEN, null);
        if (token != null) {
            mCache.put(CacheKeys.TOKEN, token);
        }
//        int addressId = sharedPreferences.getInt(CacheKeys.ADDRESS_ID, -1);
//        if (addressId > -1) {
//            mCache.put(CacheKeys.ADDRESS_ID, addressId);
//        }

//        int lastSaveStoreType = sharedPreferences.getInt(CacheKeys.LAST_SAVED_STORE_TYPE,
//                YMXMainActivity.SAVED_STORE_TYPE_ADDRESS);
//        mCache.put(CacheKeys.LAST_SAVED_STORE_TYPE, lastSaveStoreType);
//
//        if (lastSaveStoreType == YMXMainActivity.SAVED_STORE_TYPE_PICK_UP) {
//            int storeId = sharedPreferences.getInt(CacheKeys.STORE_ID, -1);
//            if (storeId > -1) {
//                mCache.put(CacheKeys.STORE_ID, storeId);
//            }
//        }
        mCache.put(CacheKeys.HAS_SHOWN_MAIN_ACTIVITY_EDUCATION,
                sharedPreferences.getBoolean(CacheKeys.HAS_SHOWN_MAIN_ACTIVITY_EDUCATION, false));
        int lastCheckedBonusId = sharedPreferences.getInt(CacheKeys.LAST_CHECKED_BONUS_ID, 0);
        mCache.put(CacheKeys.LAST_CHECKED_BONUS_ID, lastCheckedBonusId);
        int lastReadPullMessageId = sharedPreferences
                .getInt(CacheKeys.LAST_READ_PULL_MESSAGE_ID, 0);
        mCache.put(CacheKeys.LAST_READ_PULL_MESSAGE_ID, lastReadPullMessageId);
    }

    public synchronized void onMainActivityDestroy(SharedPreferences sharedPreferences) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        String token = get(CacheKeys.TOKEN);
        if (token != null) {
            editor.putString(CacheKeys.TOKEN, token);
        } else {
            editor.remove(CacheKeys.TOKEN);
        }
//        Integer addressId = get(CacheKeys.ADDRESS_ID);
//        if (addressId != null) {
//            editor.putInt(CacheKeys.ADDRESS_ID, addressId);
//        } else {
//            editor.remove(CacheKeys.ADDRESS_ID);
//        }

//        Integer lastSaveStoreType = get(CacheKeys.LAST_SAVED_STORE_TYPE);
//        if (lastSaveStoreType != null) {
//            editor.putInt(CacheKeys.LAST_SAVED_STORE_TYPE, lastSaveStoreType);
//        }
//        if (lastSaveStoreType == YMXMainActivity.SAVED_STORE_TYPE_PICK_UP) {
//            Integer storeId = get(CacheKeys.STORE_ID);
//            if (storeId != null) {
//                editor.putInt(CacheKeys.STORE_ID, storeId);
//            }
//        }

        int lastCheckedBonusId = get(CacheKeys.LAST_CHECKED_BONUS_ID);
        if (lastCheckedBonusId != 0) {
            editor.putInt(CacheKeys.LAST_CHECKED_BONUS_ID, lastCheckedBonusId);
        }
        int lastReadPullMessageId = get(CacheKeys.LAST_READ_PULL_MESSAGE_ID);
        if (lastReadPullMessageId != 0) {
            editor.putInt(CacheKeys.LAST_READ_PULL_MESSAGE_ID, lastReadPullMessageId);
        }
        editor.apply();
    }

    public synchronized <T> T get(String key) {
        if (mCache.get(key) == null) {
            return null;
        }

        if (mExpireTimeMap.containsKey(key)) {
            long expireTimeMillis = mExpireTimeMap.get(key);
            if (System.currentTimeMillis() >= expireTimeMillis) {
                remove(key);
                return null;
            }
        }

        return (T) mCache.get(key);
    }

    public synchronized void put(String key, Object object) {
//        if (key.equals(CacheKeys.CURRENT_ADDRESS)) {
//            put(CacheKeys.LAST_SAVED_STORE_TYPE, YMXMainActivity.SAVED_STORE_TYPE_ADDRESS, 0);
//        } else if (key.equals(CacheKeys.STORE_ID)) {
//            put(CacheKeys.LAST_SAVED_STORE_TYPE, YMXMainActivity.SAVED_STORE_TYPE_PICK_UP, 0);
//        }
        put(key, object, 0);
    }

    public synchronized void put(String key, Object object, long expireTimeMillis) {
        mCache.put(key, object);
        if (expireTimeMillis > 0) {
            mExpireTimeMap.put(key, System.currentTimeMillis() + expireTimeMillis);
        }
    }

    public synchronized void remove(String key) {
        mCache.remove(key);
        mExpireTimeMap.remove(key);
    }

    public String getImageUrl(String imageName, int size) {
        AppConfig config = SharedPreferencesHelper.getAppConfig();
        if (config == null || StringUtils.isEmpty(imageName)) {
            return null;
        }

        String url = config.imgUrlFormat;
        if (StringUtils.isEmpty(url)) return null;
        return url.replace("{domain}", config.staticDomain).
                replace("{image}", imageName).replace("{width}", String.valueOf(size));
    }
}
